/**
 * Copyright (C), 2015-2020, 云帐房
 * FileName: ViewDataSourceController
 * Author:   cjz
 * Date:     2020/5/12 下午7:19
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.yzf.wisecoder.controller;

import com.yzf.wisecoder.service.DataSourceService;
import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.vo.response.ViewDatasourceResp;
import com.yzf.wisecoder.wisecoderservice.service.ViewDatasourceService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ViewDatasourceEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName : ViewDataSourceController
 * @Description : 组件获取数据
 * @Author : cjz
 * @Date: 2020-05-12 19:19
 */
@RestController
@RequestMapping("sys/view")
@Api(value = "组件数据源", tags = "组件数据源")
@AllArgsConstructor
public class ViewDataSourceController {

    private DataSourceService dataSourceService;

    private ViewDatasourceService viewDatasourceService;


    @GetMapping("/data")
    @ApiOperation(value = "获取远程数据")
    public Response getDataSource(Long dataSourceId) throws Exception {

        return Response.buildSuccessResponse().setData(dataSourceService.gerRemoteById(dataSourceId));
    }

    @PostMapping("/execute")
    @ApiOperation(value = "获取远程数据2")
    public Response getDataSource2(@RequestBody Map<String,String> params) throws Exception {

        String dbId = params.get("dbId").toString();
        String execute = params.get("execute").toString();
        return Response.buildSuccessResponse().setData(dataSourceService.gerRemoteByDbIdAndExecute(Long.parseLong(dbId),execute));
    }

    /**
     * 查询数据源详细信息
     *
     * @param sourceId
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("查询组件数据源详细信息")
    public Response<ViewDatasourceResp> info(@RequestParam Long sourceId) {
        ViewDatasourceEntity viewDatasourceEntity = viewDatasourceService.getById(sourceId);
        ViewDatasourceResp viewDatasourceResp = new ViewDatasourceResp();
        BeanUtils.copyProperties(viewDatasourceEntity, viewDatasourceResp);
        return Response.buildSuccessResponseWithData(viewDatasourceResp);
    }
}
